<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="good" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 使用el-tree组件展示主题内容 -->
        <el-tree
          default-expand-all
          :data="list"
        >
          <!-- 使用作用域插槽获取组件内部的数据，可以使用提供的默认的作用域插槽  ===》 { node, data } 接收内部数据 -->
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="good" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item>添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { transDataToTreeData } from '@/utils'
export default {
  data() {
    return {
      list: [] // 保存后台返回的数据部门列表
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 加载部门列表
    async  loadDepartments() {
      try {
        const res = await getDepartments()
        console.log('getDepartments', res)
        res.data.depts.shift() // 删除第一条数据 公司的名称
        // transDataToTreeData( res.data.depts)  // 实现将平铺数据转化为树形结构的数据
        this.list = transDataToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }

}
</script>

